package com.furongji.education.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furongji.common.result.Result;
import com.furongji.common.domain.query.TeacherQuery;
import com.furongji.education.domain.po.Teacher;
import com.furongji.education.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-05-16
 */
@Tag(name = "讲师管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/courses")
public class TeacherAdminController {

    private final TeacherService service;

    @Operation(summary = "查询所有讲师")
    @GetMapping("/findAll")
    public Result<List<Teacher>> list() {
        List<Teacher> list = service.list();
        return Result.success(list);
    }

    @Operation(summary = "根据id删除讲师")
    @DeleteMapping("/{id}")
    public Result removeTeacherById(@PathVariable String id) {
        boolean flag = service.removeById(id);
        if (flag) return Result.success();
        else return Result.error();
    }

    @Operation(summary = "条件分页查询讲师列表")
    @PostMapping("/pageTeacherCondition/{current}/{size}")
    public Result<Map<String, Object>> pageQuery(@PathVariable Integer current, @PathVariable Integer size, @RequestBody(required = false) TeacherQuery teacherQuery) {
        System.out.println(current + "---------" + size);
        IPage<Teacher> page = new Page<>(current, size);
        IPage<Teacher> result = service.pageQuery(page, teacherQuery);
        Map<String, Object> map = new HashMap<>();
        map.put("total", result.getTotal());
        map.put("rows", result.getRecords());
        System.out.println("total:" + result.getTotal());
        System.out.println("rows:" + result.getRecords());
        return Result.success(map);
    }

    @Operation(summary = "添加讲师")
    @PostMapping("/addTeacher")
    public Result save(@RequestBody Teacher teacher) {
        boolean flag = service.save(teacher);
        if (flag)
            return Result.success();
        else
            return Result.error();
    }

    @Operation(summary = "根据id查询讲师")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacher(@PathVariable String id) {
        return Result.success(service.getById(id));
    }

    @Operation(summary = "根据id修改讲师")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher) {
        boolean flag = service.updateById(teacher);
        if (flag)
            return Result.success();
        else
            return Result.error();
    }
}
